/*
 * InputAttribs.java
 *
 * Created on September 8, 2007, 7:56 PM
 * 16, October, 2009: Class normalized along with the classes ExcludeColumns, ExcludeTables, InputAttribs, TimeLine and StockExperiments in order to allow the creation of an
 * abstract class AttributeFabric, in order to be conformant with design pattern method.
 * 06, April, 2011: Base class no longer inherits from class Table, rather table is now a composition object.
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.dbHost.project.attributes;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import misellanea.MiscellaneaDB;
import portfolio.dbHost.Host;
import portfolio.dbHost.dataBases.DBase;
import portfolio.dbHost.dataBases.dbTable.MysqlTable;
import portfolio.dbHost.dataBases.dbTable.OdbcTable;
import portfolio.dbHost.dataBases.dbTable.Table;

/**
 *
 * @author ACA
 */
public class InputAttribs /*extends Table*/ implements AbstractAttribute, MouseListener
{
    private Table table = null;
    
    static final public  String TABLE_NAME      = "inputattribs";
    static final private String COLUMN_INPUTDB  = "inputdb";
    static final private String COLUMN_INDEX    = "indexcolumn";
    static final private String COLUMN_TRAINSET = "trainset";
    static final private String TYPE_INPUTDB    = InputAttribs.COLUMN_INPUTDB + " CHAR(20)";
    static final private String TYPE_INDEX      = InputAttribs.COLUMN_INDEX   + " CHAR(20)";
    static final private String TYPE_TRAINSET   = InputAttribs.COLUMN_TRAINSET+ " CHAR(20)";
    
    static final public int INPUTATTRIBS_INPUTDB  = 0;
    static final public int INPUTATTRIBS_INDEXCOL = 1;
    static final public int INPUTATTRIBS_TRAINSET = 2;
    static final public int INPUTATTRIBS_SIZE     = 3;    
    
    private Vector [] vInputAttribs = null;

    /**
     * 
     */
    int mouseReleased = 0;
    public void mouseClicked(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": InputAttributes:    <<<<<<<<<<<<<<<<<<<<<<<<< Mouse Clicked >>>>>>>>>>>>>>>>>>>>>>>>>");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": InputAttributes:    <<<<<<<<<<<<<<<<<<<<<<<<< Mouse Pressed >>>>>>>>>>>>>>>>>>>>>>>>>");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": InputAttributes:    <<<<<<<<<<<<<<<<<<<<<<<<< Mouse Released>>>>>>>>>>>>>>>>>>>>>>>>>");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": InputAttributes:    <<<<<<<<<<<<<<<<<<<<<<<<< Mouse Entered >>>>>>>>>>>>>>>>>>>>>>>>>");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": InputAttributes:    <<<<<<<<<<<<<<<<<<<<<<<<< Mouse Exited  >>>>>>>>>>>>>>>>>>>>>>>>>");
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    /**
     * Creates a new instance of InputAttribs
     */
    public InputAttribs(DBase projectParam) throws Exception //, String sInputDB, String sIndexColumn, String sTrainSetColumn) throws Exception
    {
//        super (projectParam);
//        super.setName(TABLE_NAME);
        int iDBType = projectParam.getDBHost().getDBType();
        if (iDBType == Host.DB_TYPE_MYSQL) {
            table = new MysqlTable (projectParam);
        } else if (iDBType == Host.DB_TYPE_ODBC) {
            table = new OdbcTable (projectParam);
        } else {
            throw new Exception ("Unknown Data Base.");
        }
        table.setName(InputAttribs.TABLE_NAME);

        table.getDBase().setTablesVector();
        Vector vTables = table.getDBase().showTables(); //super.describe();
        if (vTables == null || !vTables.contains(TABLE_NAME)) {
            this.create ();
        }
    }
    public void initComponents (String sInputDB, String sIndexColumn, String sTrainSetColumn) throws Exception
    {
        Vector vValues = new Vector ();
        vValues.add (InputAttribs.INPUTATTRIBS_INPUTDB,  sInputDB);
        vValues.add (InputAttribs.INPUTATTRIBS_INDEXCOL, sIndexColumn);
        vValues.add (InputAttribs.INPUTATTRIBS_TRAINSET, sTrainSetColumn);
        this.insert (vValues);
    }
    /**
     *
     */
    public DefaultMutableTreeNode processHierarchy ( )
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode (TABLE_NAME);
        
        DefaultMutableTreeNode nInputDBCol = new DefaultMutableTreeNode (InputAttribs.COLUMN_INPUTDB);
        String sInputDB = vInputAttribs [InputAttribs.INPUTATTRIBS_INPUTDB].get(0).toString();
        DefaultMutableTreeNode nInputDB    = new DefaultMutableTreeNode (sInputDB);
        nInputDBCol.add(nInputDB);
        node.add(nInputDBCol);
        
        DefaultMutableTreeNode nIndexCol = new DefaultMutableTreeNode (InputAttribs.COLUMN_INDEX);
        String sIndexCol = vInputAttribs [InputAttribs.INPUTATTRIBS_INDEXCOL].get(0).toString();
        DefaultMutableTreeNode nIndex    = new DefaultMutableTreeNode (sIndexCol);
        nIndexCol.add(nIndex);
        node.add(nIndexCol);
        
        DefaultMutableTreeNode nTrainSetCol = new DefaultMutableTreeNode (InputAttribs.COLUMN_TRAINSET);
        String sTrainSet = vInputAttribs [InputAttribs.INPUTATTRIBS_TRAINSET].get(0).toString();
        DefaultMutableTreeNode nTrainSet    = new DefaultMutableTreeNode (sTrainSet);
        nTrainSetCol.add(nTrainSet);        
        node.add(nTrainSetCol);
        
        return node;
    }
    /**
     *
     * @param dbTreeParam 
     * @param iXPos 
     * @param iYPos 
     */
    public void getPopupOptions (JTree dbTreeParam, int iXPos, int iYPos)
    {
        JTree dbaseTree = dbTreeParam;       

        JPopupMenu treeOptionsMenu = new JPopupMenu ("Options");
        dbaseTree.add(treeOptionsMenu);
        
        dbaseTree.addMouseListener(this);

        treeOptionsMenu.setLocation (iXPos, iYPos);
        treeOptionsMenu.show (treeOptionsMenu, iXPos, iYPos);
        
        return;
    } /* end method getPopupOptions */      
    /**
     * Returns InputAttribs attributes, in a String array of 3 elements, distributed as follows:
     * One row and three columns of the InputAttribs Table distributed as:
     * Column 0 INPUTDB
     * Column 1 INDEXCOL
     * Column 2 TRAINSET
     * 
     */
    public String [] get ()
    {
        String [] sInputAttribs = new String [InputAttribs.INPUTATTRIBS_SIZE];
        
        sInputAttribs [InputAttribs.INPUTATTRIBS_INPUTDB]  = (String) vInputAttribs [InputAttribs.INPUTATTRIBS_INPUTDB].get(0);
        sInputAttribs [InputAttribs.INPUTATTRIBS_INDEXCOL] = (String) vInputAttribs [InputAttribs.INPUTATTRIBS_INDEXCOL].get(0);
        sInputAttribs [InputAttribs.INPUTATTRIBS_TRAINSET] = (String) vInputAttribs [InputAttribs.INPUTATTRIBS_TRAINSET].get(0);
        
        return sInputAttribs;
    }
    public Object getTreeObject (Object [] oPathParam)
    {
        if (oPathParam == null) {
            return null;
        }
        if (oPathParam.length <2) {
            return null;
        }
        if (!oPathParam [1].toString().equals(InputAttribs.TABLE_NAME)) {
           return null;  //error, no database found
        }
        
        return this;
    } /* end method getTreeObject */
    /**
     * 
     */
    public void create () throws Exception 
    {
//       super.setName (TABLE_NAME);

       table.create(InputAttribs.TYPE_INPUTDB + ", " + InputAttribs.TYPE_INDEX + ", " + InputAttribs.TYPE_TRAINSET);
    }
    /**
     * 
     */
    public void insert (Vector vValues) throws Exception 
    {
        Vector vColumns = new Vector ();
        vColumns.add (InputAttribs.COLUMN_INPUTDB);
        vColumns.add (InputAttribs.COLUMN_INDEX);
        vColumns.add (InputAttribs.COLUMN_TRAINSET);
        
        Vector vValuesTmp = new Vector ();
        vValuesTmp.add (InputAttribs.INPUTATTRIBS_INPUTDB,  "'" + vValues.get(InputAttribs.INPUTATTRIBS_INPUTDB)  + "'");
        vValuesTmp.add (InputAttribs.INPUTATTRIBS_INDEXCOL, "'" + vValues.get(InputAttribs.INPUTATTRIBS_INDEXCOL) + "'");
        vValuesTmp.add (InputAttribs.INPUTATTRIBS_TRAINSET, "'" + vValues.get(InputAttribs.INPUTATTRIBS_TRAINSET) + "'");
        
        table.insertSingleRow(vColumns, vValuesTmp);
        table.buildColumns();
    }
    /**
     *
     */
    public static Vector read (Connection connection, String sDBName) throws Exception
    {
        if (connection == null)
            return null;
        
        try {
            ResultSet resultSet = Table.getQuery(connection, sDBName, "*", InputAttribs.TABLE_NAME);
            Object [][] OMatrix = Table.transferObjectResultSet2Matrix(resultSet, 0);
            int iRowLen = OMatrix.length;
            int iColLen = OMatrix[0].length;
            String sInputDB  =  (String)OMatrix [0][InputAttribs.INPUTATTRIBS_INPUTDB];
            String sIndex    =  (String)OMatrix [0][InputAttribs.INPUTATTRIBS_INDEXCOL];
            String sTrainSet =  (String)OMatrix [0][InputAttribs.INPUTATTRIBS_TRAINSET];
            
//            System.out.println ("[[[[[[[[[[[[[[[[[[[[[[[[[[ iRowLen = " + iRowLen + "; iColLen = " + iColLen + "]]]]]]]]]]]]]]]]]]]]]]]]]]");
//            System.out.println ("[[[[[[[[[[[[[[[[[[[[[[[[[[ InputDB: " + sInputDB + "; Index: " + sIndex + "; TrainSet: " + sTrainSet + "]]]]]]]]]]]]]]]]]]]]]]]]]]");
            Vector vOutVect = new Vector ();
            vOutVect.add(sInputDB);
            vOutVect.add(sIndex);
            vOutVect.add(sTrainSet);

            return vOutVect;
        }
        catch ( SQLException sqlex ) {
            throw new Exception (sqlex);
        }
    } /* End Method readTimeLine*/
    /**
     * 
     */
    public Table getTable() {
        return table;
    }
}
